﻿using Common.Library.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Clinent.Main.View.SqlTools
{

    /// <summary>
    /// UcTSqlFormate.xaml 的交互逻辑
    /// </summary>
    public partial class UcTSqlFormate
    {
        /// <summary>
        /// 历史记录
        /// </summary>
        List<string> _record = new List<string>();

        /// <summary>
        /// 撤销历史记录
        /// </summary>
        List<string> _undoRecord = new List<string>();

        public UcTSqlFormate()
        {
            InitializeComponent();
        }

        private void UcTSqlFormate_Loaded(object sender, RoutedEventArgs e)
        {
            using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Clinent.Main.View.SqlTools.sql.xshd"))
            {
                using (var reader = new System.Xml.XmlTextReader(stream))
                {
                    aeSqlEdit.SyntaxHighlighting =
                        ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(reader,
                        ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance);
                }
            }
        }

        #region 按钮
        /// <summary>
        /// 复制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            Copy();
        }

        /// <summary>
        /// 格式化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFormate_Click(object sender, RoutedEventArgs e)
        {
            Formate();
        }

        /// <summary>
        /// 转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTransformation_Click(object sender, RoutedEventArgs e)
        {
            Transformation();
        }
        #endregion

        #region 右键菜单
        /// <summary>
        /// 撤销
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void miUndo_Click(object sender, RoutedEventArgs e)
        {
            Undo();
        }

        /// <summary>
        /// 取消撤销
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void miCancelUndo_Click(object sender, RoutedEventArgs e)
        {
            CancelUndo();
        }

        /// <summary>
        /// 复制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void miCopy_Click(object sender, RoutedEventArgs e)
        {
            Copy();
        }

        /// <summary>
        /// 格式化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void miFormate_Click(object sender, RoutedEventArgs e)
        {
            Formate();
        }

        /// <summary>
        /// 转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void miTransformation_Click(object sender, RoutedEventArgs e)
        {
            Transformation();
        }
        #endregion
               
        private void aeSqlEdit_TextChanged(object sender, EventArgs e)
        {
            if(_record.Count > 0)
            {
                if(!_record[_record.Count - 1].Equals(aeSqlEdit.Text))
                {
                    _record.Add(aeSqlEdit.Text);
                }
            }
            else
            {
                _record.Add(aeSqlEdit.Text);
            }
        }

        #region 私有方法
        /// <summary>
        /// 撤销
        /// </summary>
        private void Undo()
        {
            if(_record.Count > 0)
            {
                aeSqlEdit.Text = _record[_record.Count - 1];
                aeSqlEdit.SelectionStart = aeSqlEdit.Text.Length;
                if (_undoRecord.Count > 0)
                {
                    if (!_undoRecord[_undoRecord.Count - 1].Equals(_record[_record.Count - 1]))
                    {
                        _undoRecord.Add(_record[_record.Count - 1]);
                    }
                }
                else
                {
                    _undoRecord.Add(_record[_record.Count - 1]);
                }
                _record.RemoveAt(_record.Count - 1);
            }
        }
        
        /// <summary>
        /// 取消撤销
        /// </summary>
        private void CancelUndo()
        {
            if(_undoRecord.Count > 0)
            {
                aeSqlEdit.Text = _undoRecord[_undoRecord.Count - 1];
                aeSqlEdit.Text = _undoRecord[_undoRecord.Count - 1];
                aeSqlEdit.SelectionStart = aeSqlEdit.Text.Length;
                _undoRecord.RemoveAt(_undoRecord.Count - 1);
                //_record.RemoveAt(_record.Count - 1);
            }
        }

        /// <summary>
        /// 复制
        /// </summary>
        private void Copy()
        {
            if (aeSqlEdit.SelectedText.Length <= 0)
            {
                Clipboard.SetDataObject(aeSqlEdit.Text);
            }
            else
            {
                Clipboard.SetDataObject(aeSqlEdit.SelectedText);
            }
        }

        /// <summary>
        /// 格式化
        /// </summary>
        private void Formate()
        {
            string sql = SqlServerProfilerHelper.FormateSql(aeSqlEdit.Text);
            aeSqlEdit.Text = sql;
        }

        /// <summary>
        /// 转换
        /// </summary>
        private void Transformation()
        {
            aeSqlEdit.Text = SqlServerProfilerHelper.SpExecutesqlConvertoSql(aeSqlEdit.Text);
        }
        #endregion

        private void aeSqlEdit_KeyDown(object sender, KeyEventArgs e)
        {
            //if ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
            //    && Keyboard.IsKeyDown(Key.Z))
            //{
            //    if ((Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)))
            //    {
            //        Undo();
            //        //CancelUndo();
            //    }
            //    else
            //    {
            //        CancelUndo();
            //    }
            //    e.Handled = true;
            //}
        }
    }
}
